<?php

/**
 * Class for manipulating HTTP Request and Header
 *
 * @package OWL
 * @subpackage Helper
 * @author KeiMOon
 * @since 1.0
 */

class Owl_Http {

    /**
     * Get the referrer URL
     *
     * @return string Referrer URL
     */
    static function get_http_referrer() {
        return $_SERVER["HTTP_REFERER"];
    }

    /**
     * HTTP safe redirect
     *
     * @param string $url 
     */
    static function url_redirect($url) {
        $url = Owl_Format::esc_url($url);
        if (!headers_sent()){
            header("Status: 200");
            header("Location: $url");
        }
    }

    /**
     * Safe session creating method. Die if for some reason the session is not
     * able to create.
     */
    static function create_session() {
        if (!session_start()) {
            die("Cannot create session. Check if you have tampered the code, do NOT leave any trailing space after the php close tag.");
        }
    }

    /**
     * Get the IP address of user agent
     *
     * @return string IP address of the user agent
     */
    static function get_agent_ipaddress() {
        return $_SERVER["REMOTE_ADDR"];
    }

    /**
     * Browser detecting functions
     */
    static function get_browser() {
        $browser = new Browser();
        return array(
            "name" => $browser->getBrowser(),
            "version" => $browser->getVersion(),
            "is_mobile" => $browser->isMobile(),
            "is_robot" => $browser->isRobot()
        );
    }

    static function get_browser_name() {
        $browser = new Browser();
        return $browser->getBrowser();
    }
    
    static function get_browser_version() {
        $browser = new Browser();
        return $browser->getVersion();
    }

    static function is_mobile() {
        $browser = new Browser();
        return $browser->isMobile();
    }

    static function is_robot() {
        $browser = new Browser();
        return $browser->isRobot();
    }

    static function is_IE6() {
        $browser = new Browser();
        return ($browser->isBrowser(Browser::BROWSER_IE) && version_compare($browser->getVersion(), "7.0", "<"));
    }

    static function is_IE() {
        $browser = new Browser();
        return ($browser->isBrowser(Browser::BROWSER_IE) && version_compare($browser->getVersion(), "7.0", ">="));
    }

    static function is_Firefox() {
        $browser = new Browser();
        return $browser->isBrowser(Browser::BROWSER_FIREFOX);
    }

    static function is_Safari() {
        $browser = new Browser();
        return $browser->isBrowser(Browser::BROWSER_SAFARI);
    }

    static function is_Opera() {
        $browser = new Browser();
        return $browser->isBrowser(Browser::BROWSER_OPERA);
    }

    static function is_browser($browser_name) {
        $browser = new Browser();
        return $browser->isBrowser($browser_name);
    }
}
